package Agent;

import PacMan.SpeedTiming;
import dlife.ga.*;

public class EvolvingAgentExperiment  {
	
	/**
	* Main method
	* Lets solve this problem
	*/
	public static void main(String[] args) throws Exception {
		Individual exemplar = new Individual();
		for(int i = 0; i < SpeedTiming.NUM_GENES; i++) {
			exemplar.addGene(new DoubleGene());
		}
		
		MutationOperator mutation = new FixedRateMutation(0.01);
		CrossoverOperator cross = new OnePointCrossover();
		ParentSelector parentSelect = new RouletteWheel();
		FitnessFunction fitnessfunction = new PacmanFitnessFunction();
		
		Population pop = new FixedSizePopulation(SpeedTiming.NUM_INDIVIDUALS, exemplar, parentSelect, mutation, cross, fitnessfunction, null);
		
		PopulationStatTracker statTrack = new AveFitnessTracker(5);
		pop.addStatTracker(statTrack);
		
		pop.printStats();
		int x = 0;
		boolean done = false;
		Individual bestEver = pop.getBestIndividual();
		while(!done) {
			pop.nextGeneration();
			if(pop.getBestIndividual().getFitness() > bestEver.getFitness()) {
				bestEver = pop.getBestIndividual();
			}
			if(x % 100 == 0) {
				Individual best = pop.getBestIndividual();
				for(int i = 0; i < SpeedTiming.NUM_GENES; i++) {
					System.out.print(((DoubleGene)best.getGene(i)).getValue()+",");
				}
				System.out.println();
			}
			x++;
			
			//This line sets the number of generations to run
			if(x > SpeedTiming.NUM_GENERATIONS) {
				done = true;
			}
		}
		bestEver.write("BestIndividual.txt");
		pop.printStats();
		System.out.println("Best Ever " + bestEver.toString());
	}

}
